# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Krassy Boykinov <kboykinov@teamcentrixx.com>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=clamav
pkgver=1.2.2
pkgrel=0
pkgusers="clamav"
pkggroups="clamav"
pkgdesc="An anti-virus toolkit for UNIX eis-ng backport"
url="https://www.clamav.net/"
arch="all"
license="GPL-2.0-only"
depends="$pkgname-scanner $pkgname-daemon"
depends_dev="openssl-dev>3"
install="
	freshclam.pre-install
	clamav-daemon.pre-install
	clamav-daemon.pre-upgrade
	"
checkdepends="py3-pytest"
makedepends="
	$depends_dev
	bzip2-dev
	cargo
	check-dev
	cmake
	curl-dev
	json-c-dev
	libmilter-dev
	libmspack-dev
	libxml2-dev
	linux-headers
	musl-fts-dev
	ncurses-dev
	pcre2-dev
	python3
	samurai
	zlib-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-dev
	$pkgname-libunrar
	$pkgname-libs
	$pkgname-daemon
	$pkgname-clamdscan
	$pkgname-scanner
	$pkgname-db::noarch
	freshclam
	$pkgname-daemon-openrc:daemon_rc:noarch
	freshclam-openrc:freshclam_rc:noarch
	$pkgname-milter
	"
source="https://www.clamav.net/downloads/production/clamav-$pkgver.tar.gz
	link-fts.patch
	clamd_freshclam_conf_alpine.patch

	clamd.initd
	clamd.confd
	freshclam.initd
	freshclam.confd
	clamd.logrotate
	freshclam.logrotate
	"

case "$CARCH" in
arm*|s390x)
	# arm: tests take way too long
	# s390x: weird networking errors
	options="$options !check"
	;;
esac

# secfixes:
#   1.2.2-r0:
#     - CVE-2024-20290
#     - CVE-2024-20328
#   1.2.0-r0:
#     - CVE-2022-48579
#   1.1.1-r0:
#     - CVE-2023-20197
#   1.0.1-r0:
#     - CVE-2023-20032
#     - CVE-2023-20052
#   0.104.2-r0:
#     - CVE-2022-20698
#   0.103.2-r0:
#     - CVE-2021-1405
#     - CVE-2021-1404
#     - CVE-2021-1252
#   0.102.4-r0:
#     - CVE-2020-3350
#     - CVE-2020-3481
#   0.102.3-r0:
#     - CVE-2020-3327
#     - CVE-2020-3341
#   0.102.1-r0:
#     - CVE-2020-3123
#   0.102.0-r0:
#     - CVE-2019-15961
#   0.101.4-r0:
#     - CVE-2019-12625
#   0.100.3-r0:
#     - CVE-2019-1787
#     - CVE-2019-1788
#     - CVE-2019-1789
#   0.99.4-r0:
#     - CVE-2012-6706
#     - CVE-2017-6419
#     - CVE-2017-11423
#     - CVE-2018-0202
#     - CVE-2018-1000085
#   0.99.3-r0:
#     - CVE-2017-12374
#     - CVE-2017-12375
#     - CVE-2017-12376
#     - CVE-2017-12377
#     - CVE-2017-12378
#     - CVE-2017-12379
#     - CVE-2017-12380
#   0.100.1-r0:
#     - CVE-2017-16932
#     - CVE-2018-0360
#     - CVE-2018-0361
#   0.100.2-r0:
#     - CVE-2018-15378
#     - CVE-2018-14680
#     - CVE-2018-14681
#     - CVE-2018-14682


build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DAPP_CONFIG_DIRECTORY=/etc/clamav \
		-DDATABASE_DIRECTORY=/var/lib/clamav \
		-DENABLE_DOXYGEN=OFF \
		-DENABLE_SYSTEMD=OFF \
		-DENABLE_TESTS=ON \
		-DENABLE_CLAMONACC=ON \
		-DENABLE_MILTER=ON \
		-DENABLE_EXTERNAL_MSPACK=ON \
		-DENABLE_EXAMPLES=ON \
		-DENABLE_EXAMPLES_DEFAULT=ON \
		-DHAVE_SYSTEM_LFS_FTS=ON \
		-DENABLE_JSON_SHARED=ON
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

libunrar() {
	pkgdesc="ClamAV unrar libraries"
	replaces="clamav"
	depends=

	amove usr/lib/libclamunrar*
}

freshclam() {
	pkgdesc="Auto-updater for the Clam Antivirus scanner data-files"
	replaces="clamav clamav-db"
	depends=

	mkdir -p "$subpkgdir"/etc/clamav

	amove usr/bin/freshclam

	mv "$pkgdir"/etc/clamav/freshclam.conf.sample \
		"$subpkgdir"/etc/clamav/freshclam.conf

	install -d -m755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav

	install -m644 -D "$srcdir"/freshclam.logrotate \
		"$subpkgdir"/etc/logrotate.d/freshclam
}

freshclam_rc() {
	pkgdesc="Auto-updater for the Clam Antivirus scanner data-files (OpenRC init scripts)"
	install_if="freshclam=$pkgver-r$pkgrel openrc"
	replaces="clamav freshclam"
	depends=

	install -m755 -D "$srcdir"/freshclam.initd \
		"$subpkgdir"/etc/init.d/freshclam
	install -m644 -D "$srcdir"/freshclam.confd \
		"$subpkgdir"/etc/conf.d/freshclam
}

db() {
	pkgdesc="ClamAV dummy package for compatibility"
	replaces="clamav"
	depends="freshclam"
	mkdir -p "$subpkgdir"/var/lib/ \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/etc/clamav
	install -d -m755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav
}

scanner() {
	pkgdesc="ClamAV command-line scanner and utils"
	replaces="clamav"
	depends="freshclam"
	mkdir -p "$subpkgdir"/usr/bin

	amove usr/bin/clamscan
	amove usr/bin/sigtool
	amove usr/bin/clambc
}

clamdscan() {
	pkgdesc="ClamAV daemon client"
	depends=

	amove usr/bin/clamdscan
}

daemon() {
	pkgdesc="ClamAV daemon scanner"
	replaces="clamav"
	depends="freshclam $pkgname-clamdscan"
	mkdir -p "$subpkgdir"/etc/clamav
	install -d -m 755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav

	amove usr/bin/clamdtop
	amove usr/sbin/clamd
	amove usr/bin/clamconf

	mv "$pkgdir"/etc/clamav/clamd.conf.sample \
		"$subpkgdir"/etc/clamav/clamd.conf
	install -m644 -D "$srcdir"/clamd.logrotate \
		"$subpkgdir"/etc/logrotate.d/clamd
}

daemon_rc() {
	pkgdesc="ClamAV anti-virus scanner daemon (OpenRC init scripts)"
	replaces="clamav"
	depends="freshclam-openrc"
	install_if="$pkgname-daemon=$pkgver-r$pkgrel openrc"

	install -m755 -D "$srcdir"/clamd.initd \
		"$subpkgdir"/etc/init.d/clamd
	install -m644 -D "$srcdir"/clamd.confd \
		"$subpkgdir"/etc/conf.d/clamd
}

milter() {
	pkgdesc="ClamAV milter"
	depends=

	mkdir -p	"$subpkgdir"/etc/clamav

	amove usr/sbin/clamav-milter

	mv "$pkgdir"/etc/clamav/clamav-milter.conf.sample \
		"$subpkgdir"/etc/clamav/clamav-milter.conf
}

sha512sums="
f752496aad03efdf9e9041c8842cd6e11276a4889345e39f0d617ff046db5f90d9f7ffb3153d7ca1db3b3e452c7dd3829d4c804da73910156e827fb8d57ba230  clamav-1.2.2.tar.gz
20a52291d00e0b8e6e0cd7b31484a577045dfa8e2481fe1e4e1640e6f6d9317114c7fd53211a2bf9edcea4e084789b4820d7ceeaaf17346d581524683289e698  link-fts.patch
eea1d6e9b1949ac1dfac14a9c7dac57e43acd7a85e494f9f8fdf40eb0cd8cdd1c3054fbea6310d858fa2675e8801b10f5f569899c8ce1dec405f065b62300179  clamd_freshclam_conf_alpine.patch
d886d810de66e8da800384c1e8192f7da4352402ffc3b33cfbca93d81a2235d8c902ca9d436b9be70f00740b4555e1efbf09bf9f84059095a1a297b27581cd20  clamd.initd
59c561b3dcb0b616b647cd8e4ebc46a2cc5e7144c8c7ea0054cc1c3021d1da8f67e4dad5c083c3fe712ed887aaabfca91b538f4759537e7c4c9ab71ba4fd5794  clamd.confd
6f0c615b89f0f0d2f0e9f965f025b9ac8c81b2168fa6727dc8a47222abd780f9b656732f289d6061a20126b16126a975d50e8b3b8ff131f55dd8803da8be5dec  freshclam.initd
ba181fe1abaac7b898ccb40b0713455aa3c9d5e25ad21d687b6cac09b0105b9e376526e7c776a44636234d8db819709d8d6a6cc76119bc3e98b637b1a3f26c08  freshclam.confd
3ae493dd1610a819402c015f6b8c0f080f926b72dc43d2bded60030bf6a55040e4b88e0f64d3aae299dc1133d7e1b89855e7346b4665a64e8b82592f7b75cf6a  clamd.logrotate
30cff378bc28c76b795e00c92ae5ee623f3abe4a19bed61dd8403c96e72658bb02b7f040d26a6258104af754464d25ea7d9646918c4b47d2ba9a8cbf4687056c  freshclam.logrotate
"
